package com.da.web.dorm_storey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.da.exception.MyException;
import com.da.web.dorm_build.entity.DormBuild;
import com.da.web.dorm_build.service.DormBuildService;
import com.da.web.dorm_room.entity.DormRoom;
import com.da.web.dorm_room.service.DormRoomService;
import com.da.web.dorm_room_bed.entity.DormRoomBed;
import com.da.web.dorm_room_bed.service.DormRoomBedService;
import com.da.web.dorm_storey.entity.DormStorey;
import com.da.web.dorm_storey.mapper.DormStoreyMapper;
import com.da.web.dorm_storey.service.DormStoreyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author da
 * @time 2023/12/8 下午 12:39
 */
@Service
public class DormStoreyServiceImpl extends ServiceImpl<DormStoreyMapper, DormStorey> implements DormStoreyService {
    @Autowired
    private DormBuildService dormBuildService;
    @Autowired
    private DormRoomService dormRoomService;
    @Autowired
    private DormRoomBedService dormRoomBedService;

    @Override
    public void addDorm(List<DormStorey> list) {
        this.baseMapper.addDorm(list);
    }

    @Transactional
    @Override
    public void add(DormStorey dormStorey) {
        int insert = this.baseMapper.insert(dormStorey);
        if (insert > 0) {
            DormBuild byId = dormBuildService.getById(dormStorey.getBuildId());
            DormBuild dormBuild = new DormBuild();
            dormBuild.setBuildId(byId.getBuildId());
            dormBuild.setBuildStorey(byId.getBuildStorey() + 1L);
            dormBuildService.updateById(dormBuild);
        }
    }

    @Transactional
    @Override
    public void edit(DormStorey dormStorey) {
        int insert = this.baseMapper.updateById(dormStorey);
        //if (insert > 0) {
        //    DormBuild byId = dormBuildService.getById(dormStorey.getBuildId());
        //    DormBuild dormBuild = new DormBuild();
        //    dormBuild.setBuildId(byId.getBuildId());
        //    dormBuild.setBuildStorey(byId.getBuildStorey() - 1L);
        //    dormBuildService.updateById(dormBuild);
        //}
    }

    @Transactional
    @Override
    public void delete(Long buildId, Long storeyId) {
        int insert = this.baseMapper.deleteById(storeyId);
        if (insert > 0) {
            DormBuild byId = dormBuildService.getById(buildId);
            DormBuild dormBuild = new DormBuild();
            dormBuild.setBuildId(byId.getBuildId());
            dormBuild.setBuildStorey(byId.getBuildStorey() - 1L);
            dormBuildService.updateById(dormBuild);
        }
    }

    @Override
    public void initBed(DormStorey dormStorey) {
        //根据层数查询所有的宿舍
        QueryWrapper<DormRoom> query = new QueryWrapper<>();
        query.lambda().eq(DormRoom::getStoreyId, dormStorey.getStoreyId());
        List<DormRoom> list = dormRoomService.list(query);
        if (list.isEmpty()) throw new MyException(500, "请先设置宿舍编号");
        //循环处理宿舍的床位
        List<DormRoomBed> bedList = new ArrayList<>();
        for (DormRoom dormRoom : list) {
            //获取宿舍的人数
            int totalBed = dormRoom.getTotalBed();
            for (int j = 1; j <= totalBed; j++) {
                //查询床位是否已经设置
                QueryWrapper<DormRoomBed> queryBed = new QueryWrapper<>();
                queryBed.lambda().eq(DormRoomBed::getRoomId, dormRoom.getRoomId())
                        .eq(DormRoomBed::getBedCode, dormRoom.getRoomCode() + "-" + j); // A100-1
                DormRoomBed one = dormRoomBedService.getOne(queryBed);
                //没有查到说明还没有初始化床位
                if (one == null) {
                    DormRoomBed bed = new DormRoomBed();
                    bed.setRoomId(dormRoom.getRoomId());
                    bed.setBedCode(dormRoom.getRoomCode() + "-" + j);
                    bedList.add(bed);
                }
            }
        }
        if (!bedList.isEmpty()) {
            //批量插入数据库
            dormRoomBedService.saveBatch(bedList);
        }
    }


}
